from django.core.cache import cache
from django.shortcuts import render
import json
import re

from django.conf import settings
from django.contrib.auth import login, authenticate, logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.mail import send_mail
from django.http import HttpResponseBadRequest,HttpResponse
from django import http
from django.shortcuts import render, redirect
from django_redis import get_redis_connection

from django.views import View

from areas.models import Area
from qingmei_mall.utils.response_code import  RETCODE
from qingmei_mall.utils import  secret_openid
from users.models import User, Address


# Create your views here.

#用户地址

class AddressesView(View):

    def get(self,request):
        return render(request,'user_center_site.html')
#
# class AreasView(View):
#     """省市区数据"""
#
#     def get(self, request):
#         """提供省市区数据"""
#         area_id = request.GET.get('area_id')
#
#         if not area_id:
#             # 读取省份缓存数据
#             province_list = cache.get('province_list')
#
#             if not province_list:
#                 # 提供省份数据
#                 try:
#                     # 查询省份数据
#                     province_model_list = Area.objects.filter(parent__isnull=True)
#
#                     # 序列化省级数据
#                     province_list = []
#                     for province_model in province_model_list:
#                         province_list.append({'id': province_model.id, 'name': province_model.name})
#                 except:
#
#                     return http.JsonResponse({'code': RETCODE.DBERR, 'errmsg': '省份数据错误'})
#
#                 # 存储省份缓存数据
#                 cache.set('province_list', province_list, 3600)
#
#             # 响应省份数据
#             return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK', 'province_list': province_list})
#         else:
#             # 读取市或区缓存数据
#             sub_data = cache.get('sub_area_' + area_id)
#
#             if not sub_data:
#                 # 提供市或区数据
#                 try:
#                     parent_model = Area.objects.get(id=area_id)  # 查询市或区的父级
#                     sub_model_list = parent_model.subs.all()
#
#                     # 序列化市或区数据
#                     sub_list = []
#                     for sub_model in sub_model_list:
#                         sub_list.append({'id': sub_model.id, 'name': sub_model.name})
#
#                     sub_data = {
#                         'id': parent_model.id,  # 父级pk
#                         'name': parent_model.name,  # 父级name
#                         'subs': sub_list  # 父级的子集
#                     }
#                 except :
#
#                     return http.JsonResponse({'code': RETCODE.DBERR, 'errmsg': '城市或区数据错误'})
#
#                 # 储存市或区缓存数据
#                 cache.set('sub_area_' + area_id, sub_data, 3600)
#
#             # 响应市或区数据
#             return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK', 'sub_data': sub_data})

class AreasView(View):
    def get(self,request):
        #接受
        #response.data.sub_data.subs
        # cities,区districts,省provinces
        area_id =request.GET.get('area_id')

        #验证
        if not area_id:   #判断省份

            province=Area.objects.filter(parent__isnull=True)

            province_list=[]
            for i in province:
                dict_province={'id':i.id,'name':i.name}
                province_list.append(dict_province)

            cache.set('province_list', province_list, 3600)
            return http.JsonResponse({
                "code": "0",
                "errmsg": "OK",
                "province_list": province_list
            })

        else:
            sub_dat = cache.get('sub_area_' + area_id)
            #判断市区级
            if not sub_dat:
                parent_model = Area.objects.get(id=area_id)
                cities = parent_model.subs.all()  # 城市大全  #subs 外建引用名
                sub_model_list = []

                for j in cities:
                    dict_province = {'id': j.id, 'name': j.name}
                    sub_model_list.append(dict_province)

                sub_data = {
                    'id': parent_model.id,  # 父级pk
                    'name': parent_model.name,  # 父级name
                    'subs': sub_model_list  # 父级的子集
                }
                cache.set('sub_area_' + area_id, sub_dat, 3600)

                return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK', 'sub_data': sub_data})

        #处理
        #响应
        # return render(request,'user_center_site.html')






# [[address.receiver]]
# [[address.province]]
# [[address.city]]
# [[address.district]]
# [[address.place]]
# [[address.mobile]]
# [[address.tel]]
# [[address.email]]
